package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.dispatcher;

import org.apache.commons.lang.StringUtils;

import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.BcmcPosContext;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.dispatcher.enums.OrderDispatcherEnum;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 * 订单业务受理调度器工厂类
 *
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月18日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.dispatcher <br>
 */
public abstract class BcmcOderDispatcherFactory {

    /**
     * Description: <br>
     * 启动任务
     */
    public static void invoke(BcmcPosContext context) {
        IBcmcOrderDispatcher dispatcher = getDispatcherByOperType(context);
        dispatcher.invoke(context);
    }

    /**
     * 获取流程调度器
     *
     * @param processorEnum
     * @return
     */
    public static IBcmcOrderDispatcher getDispatcher(OrderDispatcherEnum orderDispatcherEnum) {
        return SpringUtil.getBean(orderDispatcherEnum.getClazz());
    }

    /**
     * Description: <br>
     * 根据场景操作类型获取流程调度器
     *
     * @author XXZ<br>
     * @taskId <br>
     * @param context
     * @return <br>
     */
    private static IBcmcOrderDispatcher getDispatcherByOperType(BcmcPosContext context) {
        // 默认业务变更业务
        IBcmcOrderDispatcher dispatcher = getDispatcher(OrderDispatcherEnum.ORDER_GOODS);
        // 获取场景中实例类型
        KiteSceneInst sceneInst = CuccPosServiceUtils.getSceneInst(context);
        String operType = sceneInst.getOperType();

        for (OrderDispatcherEnum dispatcherEnum : OrderDispatcherEnum.values()) {
            if (StringUtils.equals(operType, dispatcherEnum.getOperType())) {
                dispatcher = getDispatcher(dispatcherEnum);
            }
        }

        return dispatcher;
    }

}
